chrome.storage.sync.get(['redirects', 'enableRedirects'], (data) => {
    //for testing
    // data.enableRedirects = true;
    // data.redirects = [];
    // data.redirects.push({ from: 'https://www.baidu.com/', to: 'https://www.sina.com/', enabled: true });

    if (!data.enableRedirects) {
        return;
    }
    const redirects = data.redirects || [];
    redirects.forEach(redirect => {
        if (redirect.enabled) {
            const regex = new RegExp(redirect.from, 'g'); // Add 'g' flag for global replacement
            console.log('%csenew-chrome-debug Redirecting:', 'color: purple;', redirect.from, '->', redirect.to);
            // Replace the URL in the address bar
            document.querySelectorAll('a').forEach(link => {
                if (regex.test(link.href)) {
                    let href = link.href.replace(regex, redirect.to);
                    console.log('%csenew-chrome-debug Matched:', 'color: purple;', link.href, 'Replaced:', href);
                    link.href = href;
                }
            });
        }
    });
});
